package com.koron.css2.serviceManage.impl;

import com.koron.css2.AccessoryInterface;
import com.koron.css2.serviceManage.bean.BankConfigBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BankConfigMapper;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.http.HttpEntity;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 编辑银行出回盘配置
 * 
 * @author 李攀
 *
 */
public class BankConfigUpdate implements AccessoryInterface {

	private static Logger logger = LogManager.getLogger(BankConfigUpdate.class);

	@Override
	public MessageBean<?> upload(SessionFactory factory, UserInfoBean userInfo, RequestBean req, List<MultipartFile> files) {
		try {
			BankConfigBean bean = JsonUtils.objectToPojo(req.getData(), BankConfigBean.class);
			if(files == null || files.size() == 0) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "模板文件不能为空", void.class);
			}
			if(StringUtils.isEmpty(bean.getBusinessArea())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "营业不能为空", void.class);
			}
			if(bean.getChargingType() == null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "收费类型不能为空", void.class);
			}
			if(StringUtils.isEmpty(bean.getOutBank())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "出盘银行不能为空", void.class);
			}
			if(StringUtils.isEmpty(bean.getExpSql())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "导出SQL不能为空", void.class);
			}
			if(bean.getExpFileType() == null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "导出文件类型不能为空", void.class);
			}
			if(bean.getMonthidIdx() == null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "回盘账期列不能为空", void.class);
			}
			if(bean.getReturnUserNoColumn() == null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "回盘用户编号列不能为空", void.class);
			}
			if(bean.getReturnMoneyColumn() == null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "回盘金额列不能为空", void.class);
			}
			if(bean.getReturnResultColumn() == null) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "回盘结果列不能为空", void.class);
			}
			if(StringUtils.isEmpty(bean.getSuccessFlag())) {
				return MessageBean.create(Constant.ILLEGAL_PARAMETER, "扣款成功标记不能为空", void.class);
			}
			bean.setExcelTemplateFile(files.get(0).getBytes());
			bean.setBtlScriptFile(files.get(1).getBytes());
			//校验字段
			BankConfigMapper mapper = factory.getMapper(BankConfigMapper.class, DBSourceUtils.getDbEnv(bean.getCompanyNo()));
			//修改
			bean.setUpdateInfo(userInfo);
			int iCount = mapper.updateBankConfig(bean);
			if(iCount > 0) {
				logger.info("代扣出盘银行配置信息修改成功!!!");
			}
		} catch (Exception e) {
			logger.error("非法参数", e);
			return MessageBean.create(Constant.ILLEGAL_PARAMETER, "非法参数", void.class);
		}
		return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
	}

	@Override
	public HttpEntity<?> down(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		return null;
	}
}
